package com.hzw.saas.web.admin.banner.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.api.banner.IBannerService;
import com.hzw.saas.api.banner.bo.BannerBo;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.web.admin.banner.param.BannerParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

@RestController
@Validated
@RequestMapping("config")
@Api("banner配置")
public class BannerController {

    @Resource
    private IBannerService bannerService;

    @PutMapping("banner")
    @ApiOperation(value = "新增/更新banner")
    public ResponseEntity<Boolean> addOrUpdateBanner(@RequestBody BannerParam bannerParam) {
        BannerBo bannerBo = MapperUtil.nf().map(bannerParam, BannerBo.class);
        Boolean result = bannerService.addOrUpdateBanner(bannerBo);
        return ResponseEntity.ok(result);
    }

    @GetMapping("banner")
    @ApiOperation(value = "获取banner")
    public ResponseEntity<IPage<BannerBo>> getBanner(@RequestParam(value = "clientType", required = false) String clientType, PageParam pageParam) {
        IPage<BannerBo> result = bannerService.getBanner(clientType, pageParam);
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("banner/{pid}")
    @ApiOperation(value = "删除banner")
    @ApiImplicitParam(name = "pid", value = "banner主键", paramType = "path", required = true)
    public ResponseEntity<Boolean> getBanner(@PathVariable @NotBlank String pid) {
        Boolean result =  bannerService.deleteBanner(pid);
        return ResponseEntity.ok(result);
    }
}
